# -*- coding: UTF-8 -*-
# @Project ：VehicleDimensionMeasurementSystem 
# @File    ：ui_function.py
# @IDE     ：PyCharm 
# @Date    ：2024/6/12 下午7:25
from datetime import datetime

from PySide6.QtWidgets import QHeaderView, QAbstractItemView, QLineEdit

from source.database.db_operation import InitializationUiQuery
from source.modules import app_settings
from source.utils.weigh_module import get_serial_port


class UiFunctions:
    """主要页面上的展示功能类"""

    @classmethod
    def init_table_widget_style(cls, widgets):
        """初始化表格样式
        :param widgets:需要把窗体传递进来"""
        widgets.tableWidgetCompleteVehicle.verticalHeader().setVisible(False)  # 关闭垂直标题序号
        widgets.tableWidgetCompleteVehicle.horizontalHeader().setSectionResizeMode(
            QHeaderView.ResizeMode.Stretch)  # 列头自适应
        widgets.tableWidgetCompleteVehicle.setSelectionBehavior(
            QAbstractItemView.SelectionBehavior.SelectRows)  # 设置选择行行为，以行为单位

    @classmethod
    def show_drop_down_menu(cls, widgets):
        """展示下拉菜单
        :param widgets 窗体对象"""
        drop_down_item_object = InitializationUiQuery()  # 创建查询下拉菜单列表对象
        # 设置号牌号码可以拉下选择输入内容
        plate_abbr_list = drop_down_item_object.query_plate_abbreviations()
        line_edit_complete = QLineEdit()  # 用于下拉可编辑的文本框
        cls.drop_down_edit_box(widgets.comboBoxPlateNumber, line_edit_complete, plate_abbr_list)
        # 设置号牌种类的下拉框
        plate_type_list = drop_down_item_object.query_plate_type()
        widgets.comboBoxPlateType.addItems(plate_type_list)
        # 设置车辆类型的下拉框
        vehicle_type_list = drop_down_item_object.query_vehicle_type()
        widgets.comboBoxVehicleType.addItems(vehicle_type_list)
        # 设置检验类别下拉框
        inspection_category_list = drop_down_item_object.query_inspection_category()
        widgets.comboBoxInspectionClass.addItems(inspection_category_list)
        # 设置车辆所属类别下拉框
        vehicle_class_list = drop_down_item_object.query_vehicle_class()
        widgets.comboBoxVehicleClass.addItems(vehicle_class_list)

    @classmethod
    def show_current_time(cls, widgets):
        """显示当前时间"""
        widgets.current_timer.timeout.connect(
            lambda: widgets.labelCurrentTime.setText(f'当前时间：{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}'))
        widgets.current_timer.start(1000)

    @classmethod
    def show_latest_version(cls, widgets):
        """展示最新的版本"""
        widgets.labelVersion.setText(f'版本：{app_settings.VERSION}')

    @classmethod
    def show_serial_ports(cls, widgets):
        """获取当前电脑的串口进行展示"""
        serial_port_list = get_serial_port()
        widgets.comboBoxSerialPort.addItems(serial_port_list)

    @classmethod
    def set_print_report_enabled(cls, widgets):
        """设置打印按钮为不可用"""
        widgets.btnPrintReport.setEnabled(False)

    @classmethod
    def init_ui(cls, widgets):
        cls.init_table_widget_style(widgets)
        cls.show_drop_down_menu(widgets)
        cls.show_current_time(widgets)
        cls.show_latest_version(widgets)
        cls.show_serial_ports(widgets)
        cls.set_print_report_enabled(widgets)

    @staticmethod
    def drop_down_edit_box(combo_box, line_edit, item_list):
        """下拉编辑框"""
        combo_box.setLineEdit(line_edit)
        combo_box.setEnabled(True)  # 设置为可编辑的
        combo_box.addItems(item_list)  # 添加可选择的内容分
